﻿using System;

namespace PortableVersion.Core
{
    public class ConfigurationProvider : IConfigurationProvider
    {
        public static class Keys
        {
            public static readonly string InitialDate = "initialDate";
            public static readonly string MetadataFileName = "metadataFileName";
            public static readonly string ConflictFileName = "conflictFileName";
            public static readonly string MergeToolCommand = "mergeToolCommand";
            public static readonly string MergeToolCommandParams = "mergeToolCommandParams";
        }

        public T GetValue<T>(string key)
        {
            var conf = System.Configuration.ConfigurationManager.AppSettings[key];
            try
            {
                return (T)Convert.ChangeType(conf, typeof (T));
            }
            catch (InvalidCastException)
            {
                throw new InvalidCastException(String.Format(@"Error in configuration key '{0}'. Value expected to be of type {1}", key, typeof(T)));
            }
        }
    }
}
